# 视频
"""
video_info = {
    "filename": file,  # 文件名
    "url": url_path,   # 文件路径
    "hash": hash_value, # 文件哈希值，作为唯一标识
    "duration"：int,   # 视频时长
    "size": int,       # 文件大小
    "resolution",        # 分辨率：1080P、720P、480P
    "advantage": ["",""],   # 亮点：超清画质、DP、swag
    "artress":["",""], # 演员
    "combine" string  # 组合：FM、FFM、FMM、FFMM、gang
}
"""
import logging
from tinydb import where
from com.lanson.db.DatabaseManager import DatabaseManager

class VideoDAO:
    def __init__(self):
        logging.getLogger().setLevel(logging.INFO)
        self.database = DatabaseManager().load_database().get_database()  # 在初始化时加载数据库
        self.table = self.database.table("_video")  # 显式指定表操作

    def insert(self, table_info):
        """ 插入视频信息，使用hash作为主键；如果hash已经存在，则不会插入重复数据 """
        if not self.table.contains(where('hash') == table_info['hash']):
            # todo 以unicode储存，会乱码
            self.table.insert(table_info)
        else:
            logging.info(f'Video with hash {table_info["hash"]} already exists, skipping insertion.')

    def batch_insert(self, table_info_list):
        """
        批量插入视频信息列表。该方法尝试减少数据库I/O操作次数以提升性能。
        """
        for table_info in table_info_list:
            # 保持原有逻辑，检查hash是否已存在以避免重复插入
            if not self.table.contains(where('hash') == table_info['hash']):
                self.table.insert(table_info)
            else:
                logging.info(f'Video with hash {table_info["hash"]} already exists, skipping insertion in bulk operation.')

    def get_by_hash(self, hash_value):
        """根据hash获取视频信息"""
        return self.table.get(where('hash') == hash_value)

    def close(self):
        self.database.close()